package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.TaskReleaseReasonMapper;
import com.stylefeng.guns.modular.netcar.model.TaskRelease;
import com.stylefeng.guns.modular.netcar.model.TaskReleaseReason;
import com.stylefeng.guns.modular.netcar.service.ITaskReleaseReasonService;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.modular.netcar.service.ITaskReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 * 驾驶员拒绝任务或释放任务原因字典 Service 实现 
 * @author sasazaixian
 * @Date 2018-10-23 11:04
 */
@Service
public class TaskReleaseReasonServiceImpl extends ServiceImpl<TaskReleaseReasonMapper,TaskReleaseReason> implements ITaskReleaseReasonService {

    @Resource
    private TaskReleaseReasonMapper taskReleaseReasonMapper;
    @Autowired
    private ITaskReleaseService taskReleaseService;

    /**
     * 删除驾驶员拒绝任务或释放任务原因字典
     * @param taskReleaseReason
     * @return     
     */
    @Transactional
    public int deleteTaskReleaseReason(TaskReleaseReason taskReleaseReason,String token)throws Exception{
        if(taskReleaseReason==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.taskReleaseReasonMapper.deleteTaskReleaseReason(taskReleaseReason);
    }
    /**
     * 批量删除驾驶员拒绝任务或释放任务原因字典
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchTaskReleaseReason(String[] ids,String token)throws Exception{
        for(String id:ids){
            TaskRelease taskRelease=new TaskRelease();
            taskRelease.setTrrId(id);
            List list=taskReleaseService.selectListTaskRelease(taskRelease,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在释放过程中引用，不能删除！");
            }

        }
        return this.taskReleaseReasonMapper.deleteBatchTaskReleaseReason(ids);
    }
    /**
     * 查询驾驶员拒绝任务或释放任务原因字典列表
     * @param taskReleaseReason 驾驶员拒绝任务或释放任务原因字典对象
     * @return
     */
    public List<TaskReleaseReason> selectListTaskReleaseReason(TaskReleaseReason taskReleaseReason,String token)throws Exception{
        return  this.taskReleaseReasonMapper.selectListTaskReleaseReason(taskReleaseReason);
    }
    /**
	 * 分页查询驾驶员拒绝任务或释放任务原因字典列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListTaskReleaseReason(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(taskReleaseReasonMapper.selectListTaskReleaseReason(page,(TaskReleaseReason)pager.getObj()));
        List<TaskReleaseReason> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param taskReleaseReason 驾驶员拒绝任务或释放任务原因字典对象
     * @return 
     */
    public  List<TaskReleaseReason> selectListTaskReleaseReasonForUpdate(TaskReleaseReason taskReleaseReason)throws Exception{
        return  this.taskReleaseReasonMapper.selectListTaskReleaseReasonForUpdate(taskReleaseReason);
    }
    /**
     * 通过主键id查询的驾驶员拒绝任务或释放任务原因字典列表
     * @param id 主键id
     * @return     
     */
    public  TaskReleaseReason findTaskReleaseReasonById(String id,String token)throws Exception{
        return  this.taskReleaseReasonMapper.findTaskReleaseReasonById(id);
    }

    /**
     * 新增驾驶员拒绝任务或释放任务原因字典数据
     * @param taskReleaseReason  驾驶员拒绝任务或释放任务原因字典对象
     * @return
     */
    @Transactional
    public int insertTaskReleaseReason(TaskReleaseReason taskReleaseReason,String token) throws Exception { 
        if(taskReleaseReason!=null && (taskReleaseReason.getTrrId()==null || taskReleaseReason.getTrrId().trim().length()<10)){
            taskReleaseReason.setTrrId(IdWorker.get32UUID());
        }

        return this.taskReleaseReasonMapper.insertTaskReleaseReason(taskReleaseReason);
    }
    
    /**
     * 更新驾驶员拒绝任务或释放任务原因字典数据
     * @param taskReleaseReason 驾驶员拒绝任务或释放任务原因字典对象
     * @return     
     */
    @Transactional
    public int updateTaskReleaseReason(TaskReleaseReason taskReleaseReason,String token) throws Exception {     	
        return  this.taskReleaseReasonMapper.updateTaskReleaseReason(taskReleaseReason);
    }    
    /**
     * 查询驾驶员拒绝任务或释放任务原因字典返回key-Value属性
     * @param taskReleaseReason 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsTaskReleaseReason(TaskReleaseReason taskReleaseReason,String token) throws Exception {
        List<TaskReleaseReason> listTaskReleaseReason=this.taskReleaseReasonMapper.selectListTaskReleaseReason(taskReleaseReason);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(TaskReleaseReason item:listTaskReleaseReason){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getTrrId());
            opts.setValue(item.getTrrReason());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
